import { Platform, DeviceEventEmitter, BackHandler } from 'react-native';
import { Toast } from 'native-base';
import { noticeByAlert } from './notice';

// const laterBt = {
//   text: '下次再说', onPress: () => console.log('Ask me later pressed'),
// };

const cancelBt = {
  // text: '取消', onPress: () => BackHandler.exitApp(), style: 'cancel',
  text: '取消', onPress: () => console.log('Ask me later pressed'), style: 'cancel',
};

const updateBt = {
  text: '确定', onPress: () => updateApp(),
};

export const checkIsNeedUpdate = ({
  currentVersion, latestVersion, updatePolicy, updatePolicyValue,
}) => {
  if (currentVersion !== latestVersion) {
    console.info(updatePolicy);
    // 根据策略选择BT
    const bts = [cancelBt, updateBt];
    // bts.push(updateBt);
    noticeByAlert('发现新版本', `有${updatePolicyValue}${latestVersion}新版本发布,是否更新应用?`, bts);
  } else {
    // Toast.show({
    //   text: '当前是最新版本',
    //   duration: 3000,
    //   position: 'bottom',
    // });
  }
};

function updateApp() {
  if (Platform.OS === 'android') {
    // global.navigation.navigate({ routeName: 'Version' });
    DeviceEventEmitter.emit('toUpdataAppEvent');
  } else {
    // ios 跳转至appstore
  }
}
